Option Strict On
Option Explicit On

Imports System
Imports KadGen.MetadataWrapper

Public Class AdapterBase
   Private Const xmlnsNamespace As String = "http://www.w3.org/2000/xmlns/"

   Public Shared Sub LoadCustomAttributes( _
            ByVal node As Xml.XmlNode, _
            ByVal wrapper As WrapperBase, _
            ByVal ParamArray standardAttributes() As String)
      If node IsNot Nothing Then
         For Each attribute As Xml.XmlAttribute In node.Attributes
            If Array.IndexOf(standardAttributes, attribute.Name) = -1 Then
               wrapper.AddCustomAttribute(attribute.NamespaceURI, attribute.Prefix, attribute.LocalName, attribute.Value)
            End If
         Next
      End If
   End Sub

   Public Shared Sub OutputCustomAttributes( _
            ByVal wrapper As WrapperBase, _
            ByVal nsPrefix As String, _
            ByVal writer As Xml.XmlWriter)
      If wrapper.Attributes IsNot Nothing Then
         For Each attribute As KadGen.MetadataWrapper.Attribute In wrapper.Attributes
            If attribute.NamespaceUri <> xmlnsNamespace Then
               writer.WriteAttributeString(attribute.Name, attribute.NamespaceUri, attribute.Value)
            End If
         Next
      End If
      Dim docRoot As IDocumentRoot = TryCast(wrapper, IDocumentRoot)
      If docRoot IsNot Nothing Then
         OutputNamespaces(docRoot.Namespaces, nsPrefix, writer)
      End If
   End Sub

   Public Shared Sub OutputNamespaces( _
            ByVal namespaces As Attributes, _
             ByVal nsPrefix As String, _
           ByVal writer As Xml.XmlWriter)
      If namespaces IsNot Nothing Then
         For Each attribute As KadGen.MetadataWrapper.Attribute In namespaces
            If attribute.NamespacePrefix <> nsPrefix Then
               writer.WriteAttributeString("xmlns", attribute.NamespacePrefix, Nothing, attribute.NamespaceUri)
            End If
         Next
      End If
   End Sub
End Class
